'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
  async index() {
    const { ctx } = this;
    // const query
    ctx.body = await ctx.model.SystemDefine.findAll({
      limit: 1,
    });
  }

  async get() {
    const { ctx } = this;
    const { key } = ctx.params;
    const model = await ctx.model.SystemDefine.findOne({
      where: {
        key,
      },
    });
    ctx.body = model;
  }

  async add() {
    const { ctx } = this;
    const { key, value } = ctx.request.body;
    const model = await ctx.model.SystemDefine.create({ key, value });
    ctx.status = 201;
    ctx.body = model;
  }

  async update() {
    const { ctx } = this;
    const { key, value } = ctx.request.body;
    const model = await ctx.model.SystemDefine.findOne({
      where: {
        key,
      },
    });
    if (model) {
      model.value = value;
      await model.save();
    }

    ctx.body = model;
  }
}

module.exports = HomeController;
